package com.smartTerminal.user.service.impl;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.smartTerminal.user.entity.User;
import com.smartTerminal.user.service.UserService;
import com.smartTerminal.utils.StringUtils;
import com.smartTerminal.utils.XmlUtils;
import com.smartTerminal.webservice.TerminalServices;

@Service
public class UserServiceImpl implements UserService{
	
	/**
	 * 注册
	 * @method	register
	 * @author	Xia.Zhuming
	 * @data	2018年1月9日
	 * @param user	用户实体
	 * @return	userId	用户Id
	 */
	public void register(User user) throws Exception {
		//如果用户类型为空,则默认为个人用户
		if(StringUtils.isEmpty(user.getUserType())){
			user.setUserType("0");
		}
		//封装注册xml
		String userXml = XmlUtils.createXml(user);
		String userContent = TerminalServices.getInstance().getTerminalServicesHttpPort()
				.register("0", userXml);
		//获取注册状态
		JSONObject data = (JSONObject) JSONObject.parseObject(userContent);
		if(!"0".equals(data.getString("code"))){
			throw new Exception(JSONObject.parseObject(userContent).getString("message"));
		}
	}
	
	/**
	 * 身份证登录
	 * @method	login
	 * @author	Xia.Zhuming
	 * @data	2018年1月11日
	 * @param user		user对象,用户存储用户信息
	 * @param session
	 * @return
	 */
	public void login(User user, HttpSession session) throws Exception {
		if(StringUtils.isEmpty(user.getIdCard())){
			throw new Exception("-1");
		}
		String userContent = TerminalServices.getInstance().getTerminalServicesHttpPort().login(user.getIdCard());
		if(!"0".equals(JSONObject.parseObject(userContent).getString("code"))){
			throw new Exception(JSONObject.parseObject(userContent).getString("message"));
		}
		//获取登录状态
		JSONObject data = (JSONObject) JSONObject.parseObject(userContent).get("DATA");
		if(data != null){
			String userInfo = TerminalServices.getInstance().getTerminalServicesHttpPort().getUserByIdInfo(data.getString("USERNAME"));
			JSONObject userData = (JSONObject) JSONObject.parseObject(userInfo).get("DATA");
			//如果登录成功,则将登录人信息记录到实体并将实体存入session中
			user.setUserId(data.getString("USERID"));
			user.setAuthToken(data.getString("AUTHTOKEN"));
			user.setUserType(data.getString("USERTYPE"));
			user.setUserName(data.getString("REALNAME"));//接口文档中不知道是用户登录名还是用户姓名,暂时认为是用户登录名
			user.setPhone(userData.getString("MOBILE_NUM"));
			session.setAttribute("USER", user);
		}
	}
	
	/**
	 * 退出
	 * @method	loginOut
	 * @author	Xia.Zhuming
	 * @data	2018年1月9日
	 * @param authToken 凭证
	 * @return code 退出状态
	 */
	public void loginOut(HttpSession session) throws Exception {
		User user = (User) session.getAttribute("USER");
		if(user == null){
			return;
		}
		String authToken = user.getAuthToken();
		if(StringUtils.isEmpty(authToken)){
			//存在user实体但是不存在token则直接清除User实体
			session.removeAttribute("USER");
			return;
		}
		String userContent = TerminalServices.getInstance().getTerminalServicesHttpPort().loginOut(authToken);
		if(!"0".equals(JSONObject.parseObject(userContent).getString("code"))){
			throw new Exception(JSONObject.parseObject(userContent).getString("message"));
		}
		//退出成功,清除session
		session.removeAttribute("USER");
	}
}
